package xyz.lxbq.controller;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.lxbq.pojo.Major;
import xyz.lxbq.service.MajorService;
import xyz.lxbq.utils.PageUtils;
import xyz.lxbq.utils.ResultMessage;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author XuHu
 * @version 1.0
 * @date 2020/10/19 13:45
 */
@Controller
@Scope("prototype")
public class MajorController {

    @Autowired
    MajorService majorService;

    @RequestMapping("/allMajorpage/{pageIndex}/{pageSize}")
    public String getAllMajor(@PathVariable("pageIndex") int pageIndex,@PathVariable("pageSize") int pageSize,Model model){
        int i = majorService.countMajor();
        List<Major> major = majorService.getMajor((pageIndex - 1) * pageSize, pageSize);
        PageUtils pageUtils = new PageUtils(pageIndex,pageSize,i,major);
        model.addAttribute("pageUtils",pageUtils);
        return "courselist";
    }

    //学科添加
   @RequestMapping("/addMajor")
   @ResponseBody
    public ResultMessage addMajordata(Major major,HttpSession session){
        ResultMessage result=null;
       System.out.println(major);
       int i = majorService.addMajor(major);
       if(i>0){
           session.setAttribute("major",major);
            result = new ResultMessage(200, "学科增加成功");
       }else{
            result = new ResultMessage(300,"学科添加失败");
       }
       return result;
   }

   @RequestMapping("/coursedel")
   @ResponseBody
   public ResultMessage delmajordata(int id){
        ResultMessage me = null;
       System.out.println(id);
       int i = majorService.deleteById(id);
       System.out.println(i);
       if (i>0){
           me = new ResultMessage(200, "学科删除成功");
       }else{
            me =new ResultMessage(300,"学科删除失败");
       }
       return me;
   }

   @RequestMapping("/getMajor")
   @ResponseBody
    public List<Major> getNamedata(){
       List<Major> name = majorService.getName();
       System.out.println(name);
       return name;
   }

}
